package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.GoodsSubDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加商品信息...");
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @PostMapping("/sub")
    public Result sub(@RequestBody GoodsSubDTO goodsSubDTO){
        log.info("删除商品信息...");
        shoppingCartService.subShoppingCart(goodsSubDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("查询商品信息...");
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCart = shoppingCartService.list(currentId);
        return Result.success(shoppingCart);
    }
    @DeleteMapping("/clean")
    public Result clean(){
        log.info("查询商品信息...");
        Long currentId = BaseContext.getCurrentId();
        shoppingCartService.cleanShoppingCart(currentId);
        return Result.success();
    }

}
